//
//  DetailViewController.m
//  TodayFirstNews
//
//  Created by 向祖华 on 16/3/4.
//  Copyright © 2016年 向祖华. All rights reserved.
//

#import "DetailViewController.h"
#import "DetailCollectionVCell.h"

@interface DetailViewController ()<UIWebViewDelegate,UICollectionViewDelegateFlowLayout,UICollectionViewDataSource>
@property(nonatomic,strong)NSArray * titleArr;//存储底部弹框的控件标题
@property(nonatomic,strong)NSArray * imageNameArr;//存储底部弹框图片的名字
@property(nonatomic,assign)CGRect oldFrame;//存储底部弹框原来的fram
@property(nonatomic,strong)UIView * maskingView;//蒙版view
@end

@implementation DetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.bottomView.backgroundColor = [UIColor colorWithRed:220 green:220 blue:220 alpha:1];
    //设置代理
    self.webView.delegate = self;
    self.aboveCollectionV.delegate = self;
    self.aboveCollectionV.dataSource = self;
    self.belowCollectionV.delegate = self;
    self.belowCollectionV.dataSource = self;
    //webView请求详情界面
    NSURLRequest * request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.url]];
    [self.webView loadRequest:request];
    
    self.webView.scalesPageToFit = YES;//网页适配手机
    //注册cell
    [self.aboveCollectionV registerNib:[UINib nibWithNibName:@"DetailCollectionVCell" bundle:nil] forCellWithReuseIdentifier:@"DetailCollectionVCell"];
    [self.belowCollectionV registerNib:[UINib nibWithNibName:@"DetailCollectionVCell" bundle:nil] forCellWithReuseIdentifier:@"DetailCollectionVCell"];
    //处理弹框的布局及数据
    [self addBottomBox];
    
}

#pragma mark - 底部弹框的布局，添加控件
-(void)addBottomBox{
    //初始化collectionV上需要添加的内容
    self.titleArr = @[@[@"微信朋友圈",@"微信好友",@"QQ好友",@"QQ空间",@"新浪微博",@"腾讯微博",@"支付宝好友",@"支付宝生活圈",@"短信",@"邮件",@"转发链接",@"转发内容"],@[@"查看头条号",@"夜间模式",@"显示设置",@"举报"]];
    
    self.imageNameArr = @[@[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12"],@[@"13",@"14",@"15",@"16"]];
    //设置collectionView的背景色
    self.aboveCollectionV.backgroundColor = [UIColor colorWithRed:200/225.0 green:200/225.0 blue:200/225.0 alpha:1];
    self.belowCollectionV.backgroundColor = [UIColor colorWithRed:200/225.0 green:200/225.0 blue:200/225.0 alpha:1];
    
    //获取底部弹框的fram
    self.oldFrame = self.bottomView.frame;
    //添加蒙版
    self.maskingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 0)];//尺寸
    self.maskingView.backgroundColor = [UIColor blackColor];//颜色
    [self.view insertSubview:self.maskingView belowSubview:self.bottomView];//将蒙版的视图插到弹框的下面
}
/**
 *  由详情界面返回到推荐介面
 */
- (IBAction)backButtonAction:(UIButton *)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

/**
 *  分享按钮
 */
- (IBAction)shareButtonAction:(id)sender {
    
    //添加动画效果
    [UIView animateWithDuration:0.5 animations:^{
        //改变底部视图的frame
        self.bottomView.frame = CGRectMake(0, (kScreenHeight -300), kScreenWidth, (kScreenHeight - 60)/2);
        //原屏幕展现蒙版并变暗的效果
        self.maskingView.frame = CGRectMake(0, 20, kScreenWidth, kScreenHeight-20);
        self.maskingView.alpha = 0.3;
    }];

        //蒙版添加手势
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
        [self.maskingView addGestureRecognizer:tap];
}
/**
 *  蒙版轻点手势事件
 */
-(void)tapAction{
    [UIView animateWithDuration:0.5 animations:^{
        //bottom恢复隐藏
        self.bottomView.frame = self.oldFrame;
    }];
    //蒙版恢复隐藏
    self.maskingView.frame = CGRectMake(0, 20, kScreenWidth, 0);
}
/**
 *  点击底部弹框取消按钮，改变fram使底部弹框隐藏
 */
- (IBAction)bottomCancelButtonAction:(UIButton *)sender {
    [UIView animateWithDuration:0.5 animations:^{
        self.bottomView.frame = self.oldFrame;
    }];
    //蒙版恢复隐藏
    self.maskingView.frame = CGRectMake(0, 20, kScreenWidth, 0);
}



#pragma mark - collectionV的代理方法
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    if ([collectionView isEqual:self.aboveCollectionV]) {
        return 12;
    }else if ([collectionView isEqual:self.belowCollectionV]) {
        return 4;
    }else{
        return 0;
    }
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    DetailCollectionVCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DetailCollectionVCell" forIndexPath:indexPath];
    if ([collectionView isEqual: self.aboveCollectionV]) {
        cell.imageV.image = [UIImage imageNamed:self.imageNameArr[0][indexPath.row]];
        
        cell.lable.text = self.titleArr[0][indexPath.row];
    }
    
    if ([collectionView isEqual: self.belowCollectionV]) {
        cell.imageV.image = [UIImage imageNamed:self.imageNameArr[1][indexPath.row]];
        cell.lable.text = self.titleArr[1][indexPath.row];
    }
    return cell;
    
}

/**
 *  测试用
 *
 *  @param scrollView
 *  @param decelerate 
 */
//-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
//    NSLog(@"结束拖动");
//    //改变底部视图的frame
//    self.bottomView.frame = CGRectMake(0, (kScreenHeight -300), kScreenWidth, (kScreenHeight - 60)/2);
//
//}
//-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
//    NSLog(@"开始拖动");
//    //改变底部视图的frame
//    self.bottomView.frame = CGRectMake(0, (kScreenHeight -300), kScreenWidth, (kScreenHeight - 60)/2);
//}

#pragma mark - 懒加载
-(NSArray *)titleArr{
    if (!_titleArr) {
        _titleArr = [[NSArray alloc] init];
    }
    return _titleArr;
}
-(NSArray *)imageNameArr{
    if (!_imageNameArr) {
        _imageNameArr = [[NSArray alloc] init];
    }
    return _imageNameArr;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
